package me.zhengjie.ship.shipperbase.service.impl;

import me.zhengjie.ship.shipperbase.domain.ShipPerBase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.ship.shipperbase.repository.ShipPerBaseRepository;
import me.zhengjie.ship.shipperbase.service.ShipPerBaseService;
import me.zhengjie.ship.shipperbase.service.dto.ShipPerBaseDto;
import me.zhengjie.ship.shipperbase.service.dto.ShipPerBaseQueryCriteria;
import me.zhengjie.ship.shipperbase.service.mapper.ShipPerBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author liwh
* @date 2020-04-22
*/
@Service
//@CacheConfig(cacheNames = "shipPerBase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ShipPerBaseServiceImpl implements ShipPerBaseService {

    private final ShipPerBaseRepository shipPerBaseRepository;

    private final ShipPerBaseMapper shipPerBaseMapper;

    public ShipPerBaseServiceImpl(ShipPerBaseRepository shipPerBaseRepository, ShipPerBaseMapper shipPerBaseMapper) {
        this.shipPerBaseRepository = shipPerBaseRepository;
        this.shipPerBaseMapper = shipPerBaseMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ShipPerBaseQueryCriteria criteria, Pageable pageable){
        Page<ShipPerBase> page = shipPerBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(shipPerBaseMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ShipPerBaseDto> queryAll(ShipPerBaseQueryCriteria criteria){
        return shipPerBaseMapper.toDto(shipPerBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ShipPerBaseDto findById(Integer id) {
        ShipPerBase shipPerBase = shipPerBaseRepository.findById(id).orElseGet(ShipPerBase::new);
        ValidationUtil.isNull(shipPerBase.getId(),"ShipPerBase","id",id);
        return shipPerBaseMapper.toDto(shipPerBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ShipPerBaseDto create(ShipPerBase resources) {
        return shipPerBaseMapper.toDto(shipPerBaseRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ShipPerBase resources) {
        ShipPerBase shipPerBase = shipPerBaseRepository.findById(resources.getId()).orElseGet(ShipPerBase::new);
        ValidationUtil.isNull( shipPerBase.getId(),"ShipPerBase","id",resources.getId());
        shipPerBase.copy(resources);
        shipPerBaseRepository.save(shipPerBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            shipPerBaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ShipPerBaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShipPerBaseDto shipPerBase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("舷号", shipPerBase.getShipNo());
            map.put("姓名", shipPerBase.getName());
            map.put("职位", shipPerBase.getPosition());
            map.put("电话", shipPerBase.getPhone());
            map.put("备注", shipPerBase.getRemarks());
            map.put("更新时间", shipPerBase.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}